<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link href="/URMS/lib/layui-v2.5.5/css/layui.css" media="all" rel="stylesheet">
    <link href="/URMS/css/public.css" media="all" rel="stylesheet">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

<form action="" class="layui-form layuimini-form" style="text-align: center;width: 800px">


    <input name="id" th:value="${editUser.id}" type="hidden">
    <input name="status" th:value="${editUser.status}" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label required">账号</label>
        <div class="layui-input-block">
            <input class="layui-input" disabled lay-reqtext="账号不能为空" lay-verify="required" name="account"
                   placeholder="请输入账号" th:value="${editUser.account}" type="text">
            <tip>填写自己管理账号的名称。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="用户名不能为空" lay-verify="required" name="name" placeholder="请输入用户名"
                   th:value="${editUser.name}" type="text">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" id="roleid" name="roleid" placeholder="请输入" type="text">
        </div>
    </div>
    <input id="roleidList" name="roleidList" type="hidden">

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block" th:switch="${editUser.status}">
            <p th:case="'1'">
                <input checked="checked" lay-skin="switch" lay-text="ON|OFF" name="status"
                       type="checkbox">
            </p>
            <p th:case="'0'">
                <input lay-skin="switch" lay-text="ON|OFF" name="status" type="checkbox">
            </p>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
        </div>
    </div>
</form>

</div>
<script charset="utf-8" src="/URMS/lib/layui-v2.5.5/layui.js"></script>
<script charset="utf-8" src="/URMS/js/lay-config.js?v=1.0.4"></script>
<script>
    layui.use(['table', 'form', 'tableSelect'], function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#roleid',
            searchKey: 'my',
            checkedKey: 'id',
            searchPlaceholder: '自定义文字和name',

            table: {
                url: '/URMS/sysRole/selectRoleList',
                // url: '/URMS/sysRole/selectRoleList',
                cellMinWidth: 100,
                limits: [10, 15, 20],
                limit: 10,
                page: true,
                skin: 'line',
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'id', title: 'ID', width: 100},
                        {field: 'name', title: '姓名', width: 300},
                        {
                            field: 'status', title: '状态', width: 100,
                            templet: function (data) {
                                let res;
                                if (data.status == 1) {
                                    res = "<div><a class=\"layui-btn layui-btn-normal layui-btn-xs \" name='status'>可用</a></div>"
                                } else {
                                    res = "<div><a class=\"layui-btn layui-btn-xs layui-btn-danger layui-btn-disabled\" name='status'>禁用</a></div>"
                                }
                                return res;
                            }
                        }
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var roleidList = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.name)
                    roleidList.push(item.id)
                })
                $("#roleidList").val(roleidList)
                elem.val(NEWJSON.join(","))
            }
        })


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.confirm(JSON.stringify(data.field), {
                title: '最终的提交信息'

            }, function () {

                if (data.field.status == "on") {
                    data.field.status = "1";
                } else {
                    data.field.status = "0";
                }
                let user = data.field
                let roleidList = $("#roleidList").val()
                $.ajax({
                    url: "/URMS/sysUser/editSubmit?roleidList=" + roleidList,
                    data: user,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data == false) {
                            layer.msg("修改失败");
                            return false
                        } else {
                            layer.msg("修改成功")
                        }
                    },
                    error: function (XMLHttpResponse) {
                        layer.msg("登录出错");
                    }
                });

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });

            return false;
        });

    });
</script>
</body>
</html>